import pygame
from map_config import *
from map_game import *
from button_event import *
import time



pygame.init() #初始化pygame
myfont = pygame.font.Font("Yahei.ttf", text_distance) # 微软雅黑
user_name_font = pygame.font.Font("Yahei.ttf", user_text_distance) # 微软雅黑
over_win_font = pygame.font.Font("Yahei.ttf", text_distance * 2) # 微软雅黑

screen = pygame.display.set_mode((screen_width, screen_height)) #设置窗口大小

text2_board = myfont.render("2", True,text_color[0],block_color[1])
text4_board = myfont.render("4", True,text_color[1],block_color[2])
text8_board = myfont.render("8", True,text_color[2],block_color[3])
text16_board = myfont.render("16", True,text_color[3],block_color[4])
text32_board = myfont.render("32", True,text_color[4],block_color[5])

text64_board = myfont.render("64", True,text_color[5],block_color[6])
text128_board = myfont.render("128", True,text_color[6],block_color[7])
text256_board = myfont.render("256", True,text_color[7],block_color[8])
text512_board = myfont.render("512", True,text_color[8],block_color[9])
text1024_board = myfont.render("1024", True,text_color[9],block_color[10])
text2048_board = myfont.render("2048", True,text_color[10],block_color[11])



#游戏界面初始化
def game_init():
    pygame.display.set_caption("蓝萌2048 Ultra") #设置标题
    

def draw_block(value,pos_x,pos_y):
    if(value == 0):
        pygame.draw.rect(screen, block_color[0], [pos_x + start_x, pos_y + top_of_screen, box_size, box_size])

    elif(value == 2):
        pygame.draw.rect(screen, block_color[1], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text2_board,(one_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 4):
        pygame.draw.rect(screen, block_color[2], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text4_board,(one_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 8):
        pygame.draw.rect(screen, block_color[3], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text8_board,(one_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 16):
        pygame.draw.rect(screen, block_color[4], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text16_board,(two_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 32):
        pygame.draw.rect(screen, block_color[5], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text32_board,(two_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))  
                
    elif(value == 64):
        pygame.draw.rect(screen, block_color[6], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text64_board,(two_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))   

    elif(value == 128):
        pygame.draw.rect(screen, block_color[7], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text128_board,(three_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 256):
        pygame.draw.rect(screen, block_color[8], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text256_board,(three_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    elif(value == 512):
        pygame.draw.rect(screen, block_color[9], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text512_board,(three_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))        
                
    elif(value == 1024):
        pygame.draw.rect(screen, block_color[10], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text1024_board,(four_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))     
                
    elif(value == 2048):
        pygame.draw.rect(screen, block_color[11], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])
        screen.blit(text2048_board,(four_text_long + pos_x+start_x, text_distance + pos_y + top_of_screen))

    else:                
        pygame.draw.rect(screen, block_color[0], [pos_x+start_x, pos_y + top_of_screen, box_size, box_size])

#将棋盘映射到对应的格子上
def game_map_draw():
    for row in range(0,4):
        for col in range(0,4):
            value = get_block_num(row,col)
            draw_block(value,col*box_size,row*box_size)

    #画横的黑线
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen),(start_x + 4*box_size ,top_of_screen),5)
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen + box_size),(start_x + 4*box_size ,top_of_screen + box_size),5)
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen + box_size*2),(start_x + 4*box_size ,top_of_screen + box_size*2),5)
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen + box_size*3),(start_x + 4*box_size ,top_of_screen + box_size*3),5)
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen + box_size*4),(start_x + 4*box_size ,top_of_screen + box_size*4),5)    
    #画竖的黑线
    pygame.draw.line(screen,BLACK,(start_x,top_of_screen),(start_x,top_of_screen + 4*box_size),5)
    pygame.draw.line(screen,BLACK,(start_x + box_size,top_of_screen),(start_x + box_size ,top_of_screen + 4*box_size),5)
    pygame.draw.line(screen,BLACK,(start_x + 2*box_size,top_of_screen),(start_x + 2*box_size ,top_of_screen + 4*box_size),5)
    pygame.draw.line(screen,BLACK,(start_x + 3*box_size,top_of_screen),(start_x + 3*box_size ,top_of_screen + 4*box_size),5)
    pygame.draw.line(screen,BLACK,(start_x + 4*box_size,top_of_screen),(start_x + 4*box_size ,top_of_screen + 4*box_size),5)


def game_drawing():
    global myfont
    global user_name_font

    font_screen = myfont.render("2048Ultra版   AI速度:{}".format((1050 - map_game.ai_delay_time)//50), True, BLUE,BACKGROUND_COLOR)

    time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    screen.fill(BACKGROUND_COLOR) #设置背景颜色
    #screen.blit(font_screen,(80,0))
    screen.blit(font_screen,(0,0))

    game_point_board = myfont.render("得分:"+str(get_point()), True,BLUE,BACKGROUND_COLOR)
    game_point_board2 = myfont.render("步数:"+str(get_step()), True,BLUE,BACKGROUND_COLOR)  
    author_name = user_name_font.render("作者：蓝萌电子", True,BLUE,BACKGROUND_COLOR)  
    now_time = user_name_font.render(time_str, True,BLUE,BACKGROUND_COLOR)  

    screen.blit(game_point_board,(0,40))
    screen.blit(game_point_board2,(200,40))
    screen.blit(now_time,(screen_width/2 - len(time_str)/4*user_text_distance,screen_height - 3*user_text_distance))
    screen.blit(author_name,(screen_width/2 - len("作者：蓝萌电子")/2*user_text_distance,screen_height - 1.5*user_text_distance))
    #左上角起点位置(x,y) 长度 宽度 线宽
    game_map_draw()
    showButton()

    if judge_gameover() == True:
        game_over_going()
    if judge_gamewin() == True:
        game_win_going()

def game_over_going():
    global myfont
    game_over_board = myfont.render("Game Over!", True,RED,WHITE)
    screen.blit(game_over_board,(start_x + 5,top_of_screen + 5))
    game_over_board = myfont.render("Steps:" + str(get_step()), True,RED,WHITE)
    screen.blit(game_over_board,(start_x + 5,top_of_screen + 5 + box_size))
    game_over_board = myfont.render("Point:" + str(get_point()), True,RED,WHITE)
    screen.blit(game_over_board,(start_x + 5,top_of_screen + 5 + box_size * 2))


def game_win_going():
    global myfont
    game_win_board = myfont.render("Game Win!", True,GREEN,WHITE)
    screen.blit(game_win_board,(start_x + 5,top_of_screen + 5))  
    game_over_board = myfont.render("Steps:" + str(get_step()), True,GREEN,WHITE)
    screen.blit(game_over_board,(start_x + 5,top_of_screen + 5 + box_size))
    game_over_board = myfont.render("Point:" + str(get_point()), True,GREEN,WHITE)
    screen.blit(game_over_board,(start_x + 5,top_of_screen + 5 + box_size * 2))

def showButton():
    """按键显示"""
    buttonBase.update(screen)
    buttonReturn.update(screen)
    buttonAI.update(screen)
    buttonTips.update(screen)
    buttonReshow.update(screen)



